package day2Test2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

import day2test1.Student;


//1-进书、办理借书卡
//2-借书和还书功能
//3-查询某书的借阅记录
//4-查询谋卡的借阅记录
//5-按借阅次数，显示图书信息
public class Test {
	static Scanner scanner = new Scanner(System.in);
	static ArrayList<Book> books = new ArrayList<Book>();
	static ArrayList<Book> books1 = new ArrayList<Book>();

	static ArrayList<Card> cards = new ArrayList<Card>();
	static ArrayList<String> carrecord = new ArrayList<>(); // 图书卡记录
	static ArrayList<String> bookrecord = new ArrayList<>(); // 图书记录
//初始化

	public static void initialize() throws IOException {
		FileOutputStream fos1 = new FileOutputStream("books.txt");
		ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
		oos1.writeObject(books);
		oos1.flush();
		oos1.close();
		FileOutputStream fos2 = new FileOutputStream("cards.txt");
		ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
		oos2.writeObject(books);
		oos2.flush();
		oos2.close();
	}

	// 读文件 books
	public static ArrayList<Book> ReadBooksTxt() throws Exception {
		FileInputStream fis1 = new FileInputStream("books.txt");

		ObjectInputStream oin1 = new ObjectInputStream(fis1);

		books = (ArrayList<Book>) oin1.readObject();

		oin1.close();
		fis1.close();
		return books;
	}

	// 写文件 book
	public static void WirteBooksTxt(ArrayList<Book> books) throws Exception {
		FileOutputStream fos1 = new FileOutputStream("books.txt");
		ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
		oos1.writeObject(books);
		oos1.flush();
		oos1.close();
	}

	// 读文件 cards
	public static ArrayList<Card> ReadCardsTxt() throws Exception {
		FileInputStream fis2 = new FileInputStream("cards.txt");

		ObjectInputStream oin2 = new ObjectInputStream(fis2);

		cards = (ArrayList<Card>) oin2.readObject();

		oin2.close();
		fis2.close();
		return cards;
	}

	// 写文件 cards
	public static void WirteCardsTxt(ArrayList<Card> cards) throws Exception {
		FileOutputStream fos2 = new FileOutputStream("cards.txt");
		ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
		oos2.writeObject(cards);
		oos2.flush();
		oos2.close();
	}

//进书功能
	public static void inputBook() throws Exception {
		Book book = new Book();
		System.out.println("请输入书的编号");
		int id = Integer.parseInt(scanner.nextLine());
		System.out.println("请输入书名");
		String name = scanner.nextLine();
		System.out.println("请输入库存");
		int repertory = Integer.parseInt(scanner.nextLine());
		book.setId(id);
		book.setName(name);
		book.setNum(0);

		book.setRepertory(repertory);

		books = ReadBooksTxt();
		books.add(book);

		WirteBooksTxt(books);

		System.out.println("添加成功!");

	}

//办理借书卡
	public static void setCard() throws Exception {

		Card card = new Card();
		System.out.println("请输入卡号");
		int id = Integer.parseInt(scanner.nextLine());
		System.out.println("请输入持卡人姓名");
		String name = scanner.nextLine();
		card.setId(id);
		card.setName(name);

		card.setRecord(null);
		card.setNum(0);
		card.setBorrow(null);

		cards = ReadCardsTxt();
		cards.add(card);

		WirteCardsTxt(cards);

		System.out.println("办理成功");
	}

//借书功能
	public static void borrowBook() throws Exception {

		books = ReadBooksTxt();
		cards = ReadCardsTxt();
		System.out.println("请输入借书卡卡号");
		int id = Integer.parseInt(scanner.nextLine());
		int flag1 = 0;
		for (Card card : cards) { // 先查询借书卡是否舒服正确
			if (card.getId() == id) {
				flag1 = 1;
				System.out.println("请输入要借的图书姓名");
				String name = scanner.nextLine();
				int flag2 = 0;
				for (Book book : books) { // 检查该书时候在图书馆中存在
					if (name.equals(book.getName())) {
						flag2 = 1;
						if (book.getRepertory() > 0) {
							if (book.getRecord() != null) {
								carrecord = card.getRecord();
							}
							carrecord.add("借阅" + name + "一次");
							card.setRecord(carrecord);
							int num = card.getNum();

							card.setNum(num + 1);
							if (card.getBorrow() != null) {
								books1 = card.getBorrow();
							}
							books1.add(book);
							card.setBorrow(books1);

							if (book.getRecord() != null) {

								bookrecord = book.getRecord();
							}

							bookrecord.add(book.getName() + "被" + card.getName() + "借阅了一次");
							book.setRecord(bookrecord);
							int booknum = book.getNum();
							book.setNum(booknum + 1);
							int repertory = book.getRepertory();
							book.setRepertory(repertory - 1);
							System.out.println("借阅成功!");
						} else {
							System.out.println("图书库存不够，借阅失败!");
						}
						break;
					}

				}
				if (flag2 == 0) {
					System.out.println("未查询到此书，借阅失败");
				}

				break;
			}
		}
		WirteBooksTxt(books);
		WirteCardsTxt(cards);

		if (flag1 == 0) {
			System.out.println("借阅失败，借书卡卡号不存在");
		}
	}
	// 还书功能

	public static void returnBook() throws Exception {
		System.out.println("请输入借书卡卡号");
		int id = Integer.parseInt(scanner.nextLine());
		int flag1 = 0;
		int flag2 = 0;
		int flag3 = 0;

		books = ReadBooksTxt();
		cards = ReadCardsTxt();
		
		for (Card card : cards) { // 先查询借书卡是否舒服正确
			if (card.getId() == id) {
				flag1 = 1;
				System.out.println("请输入要还的图书姓名");
				String name = scanner.nextLine();

				for (Book book : books) { // 检查该书时候在图书馆中存在
					if (name.equals(book.getName())) {
						flag2 = 1;
						if (card.getBorrow() != null) {
							books1 = card.getBorrow();
							for (Book book1 : books1) {
								flag3 = 1;
								if (book1.getName().equals(name)) {
									int repertory = book1.getRepertory();

									book1.setRepertory(repertory + 1); // 三个操作。1.该书图书库存加一2.持卡人正在借阅的图书数量减一3.持卡人正在借阅图书的姓名删除
									int carnum = card.getNum();
									card.setNum(carnum - 1); // 正在借阅的书籍量减一

									books1.remove(book1);

									System.out.println("还书成功");
									break;
								}
							}
							card.setBorrow(books1);
						}

						break;
					}

				}
				break;
			}
		}

		WirteBooksTxt(books);
		WirteCardsTxt(cards);
		if (flag1 == 0) {
			System.out.println("还书失败，借书卡卡号不存在");
		}
		if (flag1 == 1 && flag2 == 0) {
			System.out.println("还书失败，图书馆不存在此书");
		}
		if (flag1 == 1 && flag2 == 1 && flag3 == 0) {
			System.out.println("还书失败，该用户未借阅此书");
		}
	}

	// 查询某书的借阅记录
	public static void findBook() throws Exception {
		System.out.println("请输入要查询的图书的名称:");
		String name = scanner.nextLine();
		int flag = 0;
		
		books = ReadBooksTxt();
		
		for (Book book : books) {
			if (name.equals(book.getName())) {
				flag = 1;
				if (book.getRecord() != null) {
					bookrecord = book.getRecord();
					System.out.println("该书的借阅记录为: " + bookrecord);
				} else {
					System.out.println("该书没有借阅记录");
				}
				int num = book.getNum();
				System.out.println("该书总共被借阅的次数为： " + num);
				break;

			}
		}
		if (flag == 0) {
			System.out.println("查询失败，此书不存在!");
		}
	}

	// 查询谋卡的借阅记录
	public static void findCard() throws Exception {
		
		
		cards = ReadCardsTxt();
		
		System.out.println("请输入借书卡的卡号");
		int id = Integer.parseInt(scanner.nextLine());
		int flag = 0;
		for (Card card : cards) {
			if (card.getId() == id) {
				flag = 1;
				String name = card.getName();
				ArrayList<String> record = card.getRecord();
				int num = card.getNum();

				System.out.println("持卡人姓名为: " + name);
				if (card.getRecord() != null) {
					record = card.getRecord();
					System.out.println("该卡的借阅记录为： " + record);
				} else {
					System.out.println("该卡没有借阅记录");
				}
				System.out.println("该卡正在借阅图书的数量为： " + num);
				if (card.getBorrow() != null) {
					books1 = card.getBorrow();
					for (Book book : books1) {

						System.out.println("该卡正在借阅图书的书籍有: 《" + book.getName() + " 》 ");
					}
				} else {
					System.out.println("没有借阅的书籍");
				}
				break;
			}
		}
		if (flag == 0) {
			System.out.println("查询失败，该借书卡不存在!");
		}
	}

	// 按借阅次数，显示图书信息
	public static void showAll() throws Exception {
		// 按照借阅次数进行排队
		books = ReadBooksTxt();
		
		for (int i = 0; i < books.size(); i++) {
			for (int j = 0; j < books.size() - i - 1; j++) {
				if (books.get(j).getNum() < books.get(j + 1).getNum()) {
					Book book1 = books.get(j);
					Book book2 = books.get(j + 1);
					books.set(j + 1, book1);
					books.set(j, book2);
				}
			}
		}
		// 遍历输出所有图书的信息
		for (Book book : books) {
			int id = book.getId();// 编号
			String name = book.getName();// 书名

			int num = book.getNum();// 借阅次数
			int repertory = book.getRepertory(); // 库存
			System.out.println("编号为：" + id);
			System.out.println("书名为：" + name);
			if (book.getRecord() != null) {
				bookrecord = book.getRecord();
				System.out.println("该书被借阅记录: " + bookrecord.toString());
			}
			System.out.println("该书被借阅次数: " + num);
			System.out.println("库存为: " + repertory);
		}
	}

	// 主界面
	public static void Meum() throws Exception {
		// 1-进书、办理借书卡
		// 2-借书和还书功能
		// 3-查询某书的借阅记录
		// 4-查询谋卡的借阅记录
		// 5-按借阅次数，显示图书信息
		while (true) {
			System.out.println("------------\t欢迎使用图书馆管理系统\t----------------");
			System.out.println("------------\t请输入命令进行功能选择\t----------------");
			System.out.println("------------\t首次使用必须初始化\t----------------");
			System.out.println("------------\t 0.初始化\t\t\t----------------");
			System.out.println("------------\t 1.进书\t\t\t----------------");
			System.out.println("------------\t 2.办理借书卡\t\t----------------");
			System.out.println("------------\t 3.借书\t\t\t----------------");
			System.out.println("------------\t 4.还书\t\t\t----------------");
			System.out.println("------------\t 5.查询借阅记录（书籍）\t----------------");
			System.out.println("------------\t 6.查询借阅记录（借书卡）\t----------------");
			System.out.println("------------\t 7.按照借阅次数显示所有图书信息----------------");
			System.out.println("------------\t #.退出\t\t\t----------------");

			String choose = scanner.nextLine();
			switch (choose) {
			case "0": {
				initialize();
				break;
			}
			case "1": {
				inputBook();
				break;
			}
			case "2": {
				setCard();
				break;
			}
			case "3": {
				borrowBook();
				break;
			}
			case "4": {
				returnBook();
				break;
			}
			case "5": {
				findBook();
				break;
			}
			case "6": {
				findCard();
				break;
			}
			case "7": {
				showAll();
				break;
			}
			case "#": {
				return;
			}
			default:
				throw new IllegalArgumentException("输入错误: " + choose);
			}
		}
	}

	public static void main(String[] args) throws Exception {
		Meum();
	}
}
